<?php

namespace App\Console\Commands;

use App\Http\Controllers\Admin\CityController;
use App\Http\Services\ASEService;
use App\Http\Services\MobileMessageService;
use App\Http\Services\OrderService;
use App\Http\Services\RSAService;
use App\Models\Card;
use App\Models\City;
use App\Models\Company;
use App\Models\CompanyCity;
use App\Models\Goods;
use App\Utils\TianZhenLan;
use Illuminate\Console\Command;
use App\Utils\RSA;
use Overtrue\Pinyin\Pinyin;

class zhang extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zhang {action?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $data = '{"venderOrderNo":"2021112910065781308","sipOrderNo":"202111291008V0115070","userStatus":"1","type":"0","useTime":"211129112046","timestamp":"1638156046"}';
        $b = 'QydevaJCsR4E0wFVur/zlS2v4reWcWbnhsSNIGV1rn+et5oeQy8fucXPfgZPuNFmulPUzW4oXaHKDu3JVvjXz7HWvPtXIN8yQog0sNyIoiN0PdoWzeoXTEOJneiI7R/OsKCtbFNkor0pxs+ilkaokkITCSupQBz4r2kAA/s7jPU=';
        $b = 'q9LjkC7evX384VfewkhExWH7Mx6DUMkwi4xoTHYRzM8v59g Ej6wxpONXrDnYUvAsj4CDIjZfnRX1kyBGakkbAk3HCAQctu\/9sRmDdcDyXjOSMjueLDpB\/iT0SkFtZpSsLBCCWg9e2tekBiFx\/rZtBS2Smn03ik7me1gUkAUeK0=';
        // $b = 'W90IHQogpdzN5izvhisgyWlkuSbBD\/2bRYSgBUKlCBjc8HmN 3\/1Cj\/0K7NhPjh7ARoo6YcGG iHhR sdD bWo 1gFa11ikLEyIuWr9h4J1ZJpKXPOzI31KIHusLXoxPJfCm8xB7EusyiW1LluwwZi6F4cd28J34pp5IAtaBmf8=';
        // $b = 'X6aAQkzgxI5zdXsJHv4b9Vds49rPuEgNhwkZcaKekF20mFKL9IMR/YdTiqj7q5/aeG4xHcMpAvd8+DzbAi/TxfadcuRV99txBvgXDRN7oHhruKNGQ1XoDRZattFKCCvKxLTThA1nuiliZPBiAflgqjmNZd7MTLj49wa5d9zi550=';
        // dd(RSAService::tz_verify($data, $b));
        dd(RSAService::tz_private_decrypt(str_replace(" ", "+", $b)));
        // OrderService::notify_sip_book_status(63);

        // $a = "2021112910065781308";
        // dd(ASEService::encrypt($a));

        $data = '{"cityId":"72","mobileNo":"13262531528","productNo":"HMT_QJF_A","sipOrderNo":"202111261827V0115045","timestamp":"1637922454","userId":"24075503"}';
        $data = "hello";

        $sign = RSAService::tz_sign($data);
        dd($sign);
        // $sign = "WekdzfuNhCRY0hkdmCIm4cHbjPp2O8XmtMYDiwYKsHHA865j/hPpBUKK38CtsHQfCDBGHtizl6JkL2w3HUtv/vjUdqbmTwkB/ElNsLLAEw0cbvg7tuYrYhpCxps/DgSlSsu6ljoRaHj9ny0pHib2lMCv7Z5uT3nN2mp3ygsDGaM=";
        echo "延签结果:".(RSAService::tz_verify($data, $sign) ? '通过' : '失败') . "\n";
        // dd($sign);
        $encData = RSAService::tz_public_encrypt($data);
        $decData = RSAService::tz_private_decrypt($encData);
        if ($decData == $data) {
            echo "加解密:通过\n";
        }
        exit;
        $method = $this->argument('action');
        if ($method) {
            return self::$method();
        }

        return OrderService::notify_sip_book_status(62);

        // dd(TianZhenLan::create_order(16));
        dd(TianZhenLan::reset_order(16));
        dd(TianZhenLan::get_order_detail('2021102810293711719'));
        dd(TianZhenLan::update_order('2021102301365569807', 1943, 1943, "2021-11-11 15:30:00", "2021-11-11 15:30:00", "李四", "王五", "15086568952", "15000000000"));
        dd(TianZhenLan::get_shop_time("1", "4", '2021-10-30', 1));
        dd(TianZhenLan::get_shops());

        dd(TianZhenLan::get_cities("1"));
        exit;
        //return self::create_card(); // 创建卡号
        return self::set_city_letter();
        // dd(ASEService::encrypt('kmccds'));
        // self::create_city_json();
        // self::rsa_test();
        // self::move_city();
        // self::set_city_id();
        return 0;
    }

    static function create_city_json() {
        // CityController::createCityJson();   // 省市区三级联
        CityController::createRegionJson(); // 只有城市的列表
    }

    static function rsa_encrypt() {
        $data = [
            "productNo" => "TZL_QJF",
            "cityType"  => "B",
            "timestamp" => "1632794947"
        ];
        $json = json_encode($data);
        $secret = RSAService::tz_public_encrypt($json);
        $sign = RSAService::tz_sign($json);
        echo $secret."\n";
        echo $sign;
    }
    static function rsa_test() {
        $encrypt =  RSAService::tz_private_encrypt('hello');
        $decrypt = RSAService::tz_public_decrypt($encrypt);

        $sign = RSAService::tz_sign('hello');
        $verify = RSAService::tz_verify('hello', $sign);

        echo $decrypt."\n";
        dd($verify);
        // $RSA = new Rsa(config('rsa.path'));
        //
        // //对数据公钥加密及私钥解密
        // $string = '快乐程序员';
        //
        // $pubString = $RSA->pubEncrypt($string);
        // echo '用公钥加密后数据:'.$pubString ."\n";
        //
        // $priDeString = $RSA->privDecrypt($pubString);
        // echo '用私钥解密数据:'.$priDeString ."\n";
        //
        // //实现对数据私钥加签及公钥验签
        //
        // $sign = $RSA->priSign($string);
        // echo '用私钥加签后得到签名:'.$sign ."\n";
        // $result = $RSA->pubVerify($string,$sign);
        // echo "验证签名是否正确:\n";
        // dump($result);
    }

    static function move_city() {
        $citys = [1,2,3,4];
        foreach($citys as $city) {
            // 新增一个中间层
            $city = City::find($city);
            $c = City::create([
                'pid'   => $city->id,
                'name'  => $city->name . '市',
                'full_name' => $city->name.$city->name.'市',
            ]);
            $c->path = $city->path . "/" .$c->id;
            $c->save();
            City::where('pid', $city->id)->update(['pid' => $c->id]);
        }
    }

    static function set_city_id() {

        $arr1 = City::city(0);
        foreach($arr1 as $a1) {
            City::where('pid', $a1->id)->update(['province_id'=>$a1->id, 'province_name'=>$a1->name]);
            City::where('id',$a1->id)->update(['province_id'=>$a1->id, 'province_name'=>$a1->name]); // 改变自己
            $arr2 = City::city($a1->id);
            foreach ($arr2 as $a2) {
                City::where('pid', $a2->id)->update(['province_id'=>$a1->id, 'province_name'=>$a1->name,'city_id'=>$a2->id, 'city_name'=>$a2->name]);
                City::where('id', $a2->id)->update(['city_id'=>$a2->id, 'city_name'=>$a2->name]);

                $arr3 = City::city($a2->id);
                foreach($arr3 as $a3) {
                    City::where('id', $a3->id)->update(['region_id'=>$a3->id, 'region_name'=>$a3->name]);
                }
            }
        }
    }

    static function set_city_letter() {
        CompanyCity::whereNull('city_letter')->chunk(1000, function($cities) {
            $pinyin = new Pinyin();
            foreach ($cities as $city) {
                $letter = strtoupper($pinyin->abbr(mb_substr($city->city_name, 0, 1)));
                $city->city_letter = $letter;
                $city->save();
            }
        });
    }

    static function set_shop_region_name() {

    }

    static function create_card() {
        $goods = Goods::all();
        foreach($goods as $g) {
            for($i=1;$i<=2;$i++){
                foreach (['A', 'B', 'C'] as $type) {
                    for ($j=0; $j<100; $j++) {
                        Card::create([
                            'company'   => $i,
                            'goods'     => $g->id,
                            'city'      => $type,
                            'code_inner'=>rand(100000, 999999),
                            'pass_inner'=>rand(100000, 999999)
                        ]);
                    }
                }
            }
        }
    }

    static function check_card() {
        $cards = Card::where('company', Company::TZL)->whereNull('use_at')->get();
        foreach($cards as $card) {
            $res = TianZhenLan::get_card($card->code);
            print_r($res->amount);
        }
    }

}
